package com.ph.elec.gwt.register.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.ph.elec.gwt.register.client.RegisterService;
import com.ph.elec.gwt.register.client.RegisterServiceAsync;
import com.ph.elec.gwt.register.client.ValidationUtils;
import com.ph.elec.gwt.util.UIUtils;
import com.ph.elec.gwt.util.UIUtils.StdDecoratorPanel;

public class ContactInfoPanel extends StdDecoratorPanel {
	
	public TextBox txtEmail = new TextBox();
	public TextBox txtTelephone = new TextBox();
	public TextBox txtMobile = new TextBox();

	public ContactInfoPanel() {
		vtpRoot.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtEmail, "250px", 30), 
				"Email Address", 
				false));
		txtEmail.setEnabled(false);

		HorizontalPanel hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtTelephone, "125px", 20), 
				"Telephone No.", 
				false));
		
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtMobile, "150px", 25), 
				"Mobile No.", 
				true));
		vtpRoot.add(hzpRow);
		
		add(vtpRoot);
		setWidth("500px");
		
		initializeContents();
	}

	private void initializeContents() {
		RegisterServiceAsync registerSv = GWT.create(RegisterService.class);
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(String result) {
				txtEmail.setText(result);
			}
		};
		registerSv.GetEmail(callback);
	}

	public boolean validate() {
		StringBuffer message = new StringBuffer();
		
		message.append(ValidationUtils.checkMobileNo(txtMobile.getText()));
		if (message.length() > 0) {
			addMultipleErrorMessage(message.toString());
			return false;
		}
		return true;
	}
}
